// 敌机
function Enemy(obj) {
    this.init = function() {
        // 敌机的宽高
        this.width = obj.width;
        this.height = obj.height;

        // 敌机在canvas上的坐标
        this.x = Tool.rand(0, canvas.width - this.width);
        this.y = -this.height;

        // 敌机移动速度
        this.moveX = 2;
        this.moveY = 2;

        // 绘制敌机
        this.img = obj.img;

        // 敌机是否展示
        this.isShow = true;

        // 生命值
        this.life = obj.life;
    };
    this.init();

    this.draw = function() {
        if (this.isShow) {
            // 敌机下落,改变纵向的y的值
            this.move();

            // 绘制敌机
            ctx.drawImage(this.img, this.x, this.y);
        }
    };

    this.move = function() {
        this.y += this.moveY;

        if (this.y > canvas.height) {
            this.isShow = false;
        }
    }
}

function EnemySet() {
    this.init = function() {
        var time = new Date().getTime();

        // 敌机集合
        this.set = [];

        // 敌机数据
        this._enemy = {
            big: { width: 259, height: 196, img: Tool.getImage('./images/dj3.png'), lasttime: time, interval: 19000, life: 5 },
            middle: { width: 177, height: 135, img: Tool.getImage('./images/dj2.png'), lasttime: time, interval: 8000, life: 3 },
            small: { width: 106, height: 76, img: Tool.getImage('./images/dj1.png'), lasttime: time, interval: 3000, life: 1 }
        }
    };
    this.init();

    this.create = function() {
        // 获取新时间
        var newtime = new Date().getTime();

        // 生成新的敌机
        for (var index in this._enemy) {
            var item = this._enemy[index];
            // 判断是否该绘制新敌机
            if (newtime - item.lasttime > item.interval) {
                // 生成新的子弹
                var newEnemy = new Enemy(item);

                // 并且推入到数组中
                this.set.push(newEnemy);

                // 并更新最后一次生成时间
                item.lasttime = newtime;
            }
        }
    };

    this.draw = function() {
        // 创建敌机
        this.create();

        // 循环敌机进行绘制
        for (var i = 0; i < this.set.length; i++) {
            // 子弹展示，则绘制
            if (this.set[i].isShow) {
                this.set[i].draw();
            } else {
                // 子弹已经出界，删除该元素
                this.set.splice(i, 1);
            }
        }
    }
}